class HomeController < ApplicationController
  def index
    @recent_complaints=Complaint.recent
  end
  
  def visualization
    vis_type=params[:tq]
    rows=[]
    if vis_type=="month"
      columns=[{:id=>'week',:label=>'Week',:type=>'string'},{:id=>'total_complaints',:label=>'Complaints',:type=>'number'}]
      complaints_by_week=Complaint.all.group_by(&:week)
      complaints_by_week.each_pair do |k,v|
        rows << {:c=>[{:v=>k.to_s},{:v=>v.size}]}
      end
    elsif vis_type=="company"
      columns=[{:id=>'company',:label=>'Company',:type=>'string'},{:id=>'total_complaints',:label=>'Complaints',:type=>'number'}]
      complaints_by_company=Company.all.map{|c| {c.title,c.complaints.size}}
      complaints_by_company.each do |c|
        c.each_key do |k|
          rows << {:c=>[{:v=>k.to_s},{:v=>c[k].to_i}]}
        end
      end
    else
    end
    request_id=params[:tqx].split(':').last.to_i
    table={:table=>{:cols=>columns,:rows=>rows}}
    google_vis={:version =>0.6, :reqId=>request_id, :status=>'OK'}.merge(table)
    render :text=>"google.visualization.Query.setResponse(#{google_vis.to_json})"
  end
 
end
